package com.whpost.service.wx.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whpost.service.entity.t.TCzy;
import com.whpost.service.entity.tools.ListInfo;
import com.whpost.service.entity.tools.PageInfo;
import com.whpost.service.entity.tools.ResultInfo;
import com.whpost.service.service.BaseServices;
import com.whpost.service.service.WxBaseServices;
import com.whpost.service.wx.dao.WxBaomingMapper;
import com.whpost.service.wx.entiy.WxBaoming;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WxbmServices extends BaseServices<WxBaomingMapper, WxBaoming> {

    @Override
    public ResultInfo query(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            TCzy tCzy = gettCzyDao().selectById(loginId);

            String curpage = "",pagesize="",sfdc="",xm="";
            if (jsonObject.containsKey("curpage")) {
                curpage = jsonObject.getString("curpage");
            }
            if (jsonObject.containsKey("pagesize")) {
                pagesize = jsonObject.getString("pagesize");
            }
            if (jsonObject.containsKey("xm")) {
                xm = jsonObject.getString("xm");
            }
            if (jsonObject.containsKey("sfdc")) {
                sfdc = jsonObject.getString("sfdc");
            }

            if (StringUtils.isEmpty(curpage)) curpage = "1";
            if (StringUtils.isEmpty(pagesize)) pagesize = "10";

            QueryWrapper<WxBaoming> queryWrapper = new QueryWrapper<>();


            Page<WxBaoming> page = new Page<>( Integer.parseInt(curpage),  Integer.parseInt(pagesize));

            if ("1".equals(sfdc)){
                page = new Page<>(Integer.parseInt(curpage),  Integer.MAX_VALUE);
            }

            ListInfo<WxBaoming> wxBaomingListInfo = new ListInfo<>();
            if (!StringUtils.isEmpty(xm))
            {
                queryWrapper.like("xm",xm);
            }
            queryWrapper.eq("1","1");
            queryWrapper.orderByAsc("id");

            IPage<WxBaoming> wxBaomingIPage = getBaseMapper().selectPage(page,queryWrapper);

            PageInfo pageInfo = new PageInfo();
            pageInfo.setTotal(wxBaomingIPage.getTotal());
            pageInfo.setCurPage(wxBaomingIPage.getCurrent());
            pageInfo.setPageSize(wxBaomingIPage.getSize());
            pageInfo.setPageTotal(wxBaomingIPage.getPages());

            wxBaomingListInfo.setPageInfo(pageInfo);
            wxBaomingListInfo.setListInfo(wxBaomingIPage.getRecords());
            wxBaomingListInfo.setUserId(loginId);
            wxBaomingListInfo.setXzjgId(String.valueOf(tCzy.getXzjgId()));

            //查询条件的值
            Map<String,Object> cxtjMap = new HashMap<>();
            cxtjMap.put("xm",xm);

            //返回前端的内容
            Map<String,Object> rtnMap = new HashMap<>();
            rtnMap.put("cxjg",wxBaomingListInfo);
            rtnMap.put("cxtj",cxtjMap);

            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",rtnMap);

            if ("1".equals(sfdc)){
                return resultInfo;
            }else{
                return backRtn(resultInfo,loginId);
            }
        }
    }
}
